//
//  AddressManageCell.m
//  Ufan
//
//  Created by yangqingluo on 15/12/10.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "AddressManageCell.h"
#import "UIResponder+Router.h"

@interface AddressManageCell()

@property (strong, nonatomic) UIButton *selectedButton;
@property (strong, nonatomic) UIView *bottomLineView1;
@property (strong, nonatomic) UILabel *addressLabel;
@property (strong, nonatomic) UILabel *noteLabel;

@end

@implementation AddressManageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textLabel.font = [UIFont systemFontOfSize:16.0];
        
        self.detailTextLabel.font = self.textLabel.font;
        self.detailTextLabel.textColor = self.textLabel.textColor;
        self.detailTextLabel.textAlignment = NSTextAlignmentRight;
        
        self.addressLabel = [[UILabel alloc]init];
        self.addressLabel.font = [UIFont systemFontOfSize:12.0];
        self.addressLabel.textColor = self.textLabel.textColor;
        [self.contentView addSubview:self.addressLabel];
        
        self.noteLabel = [UILabel new];
        self.noteLabel.font = self.addressLabel.font;
        self.noteLabel.textColor = [UIColor orangeColor];
        [self.contentView addSubview:self.noteLabel];
        
        _bottomLineView1 = [[UIView alloc] init];
        _bottomLineView1.backgroundColor = UFCOLOR_GRAYLINE;
        [self.contentView addSubview:_bottomLineView1];
        
        _selectedButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectedButton.frame = CGRectMake(0, 0, 20, 20);
        [_selectedButton setImage:[UIImage imageNamed:@"public_check_unselected"] forState:UIControlStateNormal];
        [_selectedButton setImage:[UIImage imageNamed:@"public_check_selected"] forState:UIControlStateSelected];
        _selectedButton.userInteractionEnabled = NO;
        [self.contentView addSubview:_selectedButton];
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOccure)];
//        [self.contentView addGestureRecognizer:tap];
    }
    return self;
}

- (void)tapOccure{
    if (self.contentView.frame.origin.x > 0) {
        [self routerEventWithName:UF_Router_Event_AddressSelect userInfo:nil];
    }
    else{
        
    }
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
    if (self.contentView.frame.origin.x > 0) {
        [self routerEventWithName:UF_Router_Event_AddressSelect userInfo:@{@"info":self.address}];
    }
    else{
        [super touchesBegan:touches withEvent: event];
    }
    
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _bottomLineView1.frame = CGRectMake(15, self.contentView.height - 1, self.contentView.width - 30, 1);
    
    
    self.textLabel.text = self.address.deliveryName;
    self.detailTextLabel.text = starMiddle(self.address.deliveryPhone);
    
    NSString *noteString = [NSString stringWithFormat:@"%@[%@]",(self.address.def ? @"[默认]" : @""),self.address.name];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",noteString,self.address.detailAddress]];
    [attString addAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor],NSFontAttributeName:self.addressLabel.font} range:NSMakeRange(0, noteString.length)];
    [attString addAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:self.addressLabel.font} range:NSMakeRange(noteString.length , attString.length - noteString.length)];
    self.addressLabel.attributedText = attString;
    
    self.textLabel.frame = CGRectMake(_bottomLineView1.left, 15, 0.5 * _bottomLineView1.width, 20);
    self.detailTextLabel.frame = CGRectMake(self.textLabel.right, self.textLabel.top, 0.5 * _bottomLineView1.width - 40, self.textLabel.height);
    
    
    
    self.addressLabel.frame = CGRectMake(self.textLabel.left, self.textLabel.bottom, _bottomLineView1.width, self.textLabel.height);
    
    self.selectedButton.selected = [[AppPublic shareInstanceApp].addressSelected.idString isEqualToString:self.address.idString];
    self.selectedButton.center = CGPointMake(_bottomLineView1.right - 0.5 * self.selectedButton.width, 0.5 * self.contentView.height);
    
    _bottomLineView1.frame = CGRectMake(0, self.contentView.height - 1, self.contentView.width, 1);
}


+(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

@end
